package com.wnxy.sddfp.gateway.config.cors;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 全局跨域配置
 */
@Configuration
public class GlobalCorsConfig {
    /**
     * 网关跨域配置
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowCredentials(true);
        //允许来自所有的域名（来源）进行跨域访问
        configuration.addAllowedOrigin("*");
        //允许所有的方法进行跨域访问 get  post  delete put option
        configuration.addAllowedMethod("*");
        //允许携带请求头
        configuration.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //对所有请求的资源（请求路径）生效
        source.registerCorsConfiguration("/**",configuration);
        return new CorsWebFilter(source);
    }
}